#include "deviceview.h"
#include "ui_deviceview.h"

DeviceView::DeviceView(QWidget* parent)
    : QWidget(parent),
    ui(new Ui::DeviceView()),
    controlsLayout1(new QHBoxLayout),
    controlsLayout2(new QHBoxLayout),
    centralLayout(new QVBoxLayout(this)),
    cntLayout(0)
{
    ui->setupUi(this);

    controlsLayout1->setSpacing(0);
    controlsLayout1->setMargin(0);

    controlsLayout2->setSpacing(0);
    controlsLayout2->setMargin(0);

    centralLayout->setSpacing(1);
    centralLayout->setMargin(0);
    centralLayout->addLayout(controlsLayout1);
    centralLayout->addLayout(controlsLayout2);
}

DeviceView::~DeviceView()
{
    delete ui;
}

void DeviceView::addView(const QString& name)
{
    switch (cntLayout)
    {
    case 0:
    {
        auto view1 = std::make_shared<DeviceSingleView>(this, name);
        views.insert(name, view1);
        controlsLayout1->addWidget(view1.get(), 1);
        cameraLayout[name] = controlsLayout1;
        cntLayout++;
        break;
    }
    case 1:
    {
        auto view2 = std::make_shared<DeviceSingleView>(this, name);
        views.insert(name, view2);
        controlsLayout1->addSpacing(1);
        controlsLayout1->addWidget(view2.get(), 1);
        cameraLayout[name] = controlsLayout1;
        cntLayout++;
        break;
    }
    case 2:
    {
        auto view3 = std::make_shared<DeviceSingleView>(this, name);
        views.insert(name, view3);
        controlsLayout2->addWidget(view3.get(), 1);
        cameraLayout[name] = controlsLayout2;
        cntLayout++;
        break;
    }
    case 3:
    {
        auto view4 = std::make_shared<DeviceSingleView>(this, name);
        views.insert(name, view4);
        controlsLayout2->addSpacing(1);
        controlsLayout2->addWidget(view4.get(), 1);
        cameraLayout[name] = controlsLayout2;
        cntLayout++;
        break;
    }
    default:
        break;
    }
}

void DeviceView::removeView()
{
    switch (cntLayout)
    {
    case 4:
        controlsLayout2->removeWidget(views.values().at(3).get());
    case 3:
        controlsLayout2->removeWidget(views.values().at(2).get());
    case 2:
        controlsLayout1->removeWidget(views.values().at(1).get());
    case 1:
        controlsLayout1->removeWidget(views.values().at(0).get());
        break;
    default:
        break;
    }

    views.clear();
    cntLayout = 0;
}

void DeviceView::removeCurView(const QString& name)
{
    auto view = views.find(name);
    if (view != views.end())
    {
        auto view = views[name];
        if (view != nullptr) view.get()->hide();
        else return;
    }
    return;
}

QStringList DeviceView::getViewNames()
{
    return views.keys();
}

void DeviceView::updateImage(const QString& name, const QImage& image)
{
    if (views.contains(name))
    {
        auto view = views[name];
        if (view)
        {
            view->updateImagec(image);

            view->clearError();
        }
    }
    else
    {
        addView(name);
        auto view = views[name];
        if (view)
        {
            view->updateImagec(image);
        }
    }
}

bool DeviceView::saveImage(const QString& name, const QString& filePath, const QString& imageFormat, int imageQuality)
{
    bool res = false;
    auto view = views[name];

    if (view != nullptr)
    {
        res = view->saveImage(filePath, imageFormat, imageQuality);
    }

    return res;
}

void DeviceView::setError(const QString& name, const QString& msg)
{
    auto view = views[name];

    if (view != nullptr)
    {
        view->setError(msg);
    }
}

QString DeviceView::getError(const QString& name)
{
    auto view = views[name];
    QString msg("");

    if (view != nullptr)
    {
        msg = view->getError();
    }

    return msg;
}

void DeviceView::clearError(const QString& name)
{
    auto view = views[name];

    if (view != nullptr)
    {
        view->clearError();
    }
}

bool DeviceView::isError(const QString& name)
{
    auto view = views[name];
    bool isError = false;

    if (view != nullptr)
    {
        isError = view->isError();
    }
    return isError;
}

void DeviceView::setPath(const QString& name, const QString& path)
{
    auto view = views[name];

    if (view != nullptr)
    {
        view->setPath(path);
    }
}

QString DeviceView::getPath(const QString& name)
{
    auto view = views[name];
    QString path("");

    if (view != nullptr)
    {
        path = view->getPath();
    }

    return path;
}

void DeviceView::updateImageD(const QString& name, const QImage& image)
{
    if (views.contains(name))
    {
        auto view = views[name];
        if (view)
        {
            view.get()->show();
            view->updateImaged(image);
        }
    }
    else
    {
        addView(name);
        auto view = views[name];
        if (view)
        {
            view->updateImaged(image);
        }
    }
}

void DeviceView::updateImageS(const QString& name, const QImage& image)
{
    if (views.contains(name))
    {
        auto view = views[name];
        if (view)
        {
            view.get()->show();
            view->updateImages(image);
        }
    }
    else
    {
        addView(name);
        auto view = views[name];
        if (view)
        {
            view->updateImages(image);
        }
    }
}

void DeviceView::updateImageH(const QString& name, const QImage& image)
{
    if (views.contains(name))
    {
        auto view = views[name];
        if (view)
        {
            view.get()->show();
            view->updateImageh(image);
        }
    }
    else
    {
        addView(name);
        auto view = views[name];
        if (view)
        {
            view->updateImageh(image);
        }
    }
}

void DeviceView::updateImageDMD(const QString& name, const QImage& image)
{
    if (views.contains(name))
    {
        auto view = views[name];
        if (view)
        {
            view.get()->show();
            view->updateImagedmd(image);
        }
    }
    else
    {
        addView(name);
        auto view = views[name];
        if (view)
        {
            view->updateImagedmd(image);
        }
    }
}
